﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
namespace HZ.Supervise.Web.Secondphase.ReportArea
{
	public partial class CurrentGoodsList : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{

				ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
				SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
				if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
				{
                    if (sq.SortSource == 0)         //上个页面传过来的session，然后赋给本页面的dropSort。
                    {
                        droSort.SelectedIndex = 1;
                    }
                    else if (sq.SortSource == 1)
                    {
                        droSort.SelectedIndex = 2;
                    }
                    else if (sq.SortSource == 2)
                    {
                        droSort.SelectedIndex = 3;
                    }
					BindList();
					GetStatistics();
				}
				else
				{
					Response.Write("<script>history.go(-1);<script>");
				}
			}
		}

		void BindList()
		{
			//////////////////////////////////////////////////////////////////
			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            string sortsource = Convert.ToString(sq.SortSource);
            //var model = new HZ.Data.QueryModel();
            //model["AreaId"] = sq.AreaID;
            //model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                //model["SortSorce"] = droSort.SelectedValue;
                sortsource = droSort.SelectedValue;
            }
            else
            {
                sortsource = "-1";
            }

            //model["SortFields"] = this.droSortField.SelectedValue;

			int t = 0;

			HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

            System.Data.DataTable dt = ie.ExecutePageFirst(1, 0, out t, sortsource, droSortField.SelectedValue, sq.AreaID, sq.Mid);
			HZ.Supervise.Web.Function.AddColumn(dt, "SortSource");
			HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
			gvwSortInfo.DataSource = dt;
			gvwSortInfo.DataBind();

			HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 0);
			HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 7);
		}
		void GetStatistics()
		{
			double purchaseAmountTotal = 0;//采购总金额
			double wareHouseAmountTotal = 0;//入库总金额
			double distributeAmountTotal = 0;//配送总金额

			for (int i = 0; i < gvwSortInfo.Rows.Count; i++)
			{
				purchaseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[2].Text);
				wareHouseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[5].Text);
				distributeAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[3].Text);
			}
			this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributeAmountTotal.ToString() + " ;入库总金额为:" + wareHouseAmountTotal.ToString();

		}

		private string SubstringAreaID(string AreaID)
		{
			if (!string.IsNullOrEmpty(AreaID) && AreaID.Length > 4)
			{
				if (AreaID.Substring(4) == "00")
				{
					AreaID = AreaID.Substring(0, 4) + "%";
				}
				else
				{
					AreaID += "%";
				}
			}

			return AreaID;
		}

		/// <summary>
		/// 合并表头
		/// 张雄 create 2011.4.27
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
		{
			#region 合并表头
			switch (e.Row.RowType)
			{
				case DataControlRowType.Header:

					//取得下拉框的月份
					//第一行表头
					TableCellCollection tcHeader = e.Row.Cells;
					tcHeader.Clear();
					tcHeader.Add(new TableHeaderCell());
					tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
					tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
					SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

					string areaname = HZ.Supervise.BLLFactory.IBLL_Area().GetArea(sq.AreaID).AreaName;

					if (!string.IsNullOrEmpty(areaname))
					{
						tcHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + "  " + areaname + "</th></tr><tr>";
					}
					else
					{
						tcHeader[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + "</th></tr><tr>";
					}
					//第2行表头
					tcHeader.Add(new TableHeaderCell());
					tcHeader[1].Attributes.Add("bgcolor", "Khaki");
					tcHeader[1].Text = "地区";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[2].Attributes.Add("bgcolor", "Khaki");
					tcHeader[2].Text = "采购类型";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[3].Attributes.Add("bgcolor", "Khaki");
					tcHeader[3].Text = "采购总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[4].Attributes.Add("bgcolor", "Khaki");
					tcHeader[4].Text = "配送总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[5].Attributes.Add("bgcolor", "Khaki");
					tcHeader[5].Text = "配送率";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[6].Attributes.Add("bgcolor", "Khaki");
					tcHeader[6].Text = "入库总金额";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[7].Attributes.Add("bgcolor", "Khaki");
					tcHeader[7].Text = "入库率";

					tcHeader.Add(new TableHeaderCell());
					tcHeader[8].Attributes.Add("bgcolor", "Khaki");
					tcHeader[8].Text = "操作</th></tr><tr>";
					break;
			}
			#endregion
		}

		protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
		{
 
		}

		protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
		{

			if (e.CommandName == "DirectByDetail")
			{
				string sessionid = Guid.NewGuid().ToString();
				SuperviseQuery query = (SuperviseQuery)ViewState["query"];
				query.SortSource = Convert.ToInt32(this.droSort.SelectedValue);
				query.AreaName = e.CommandArgument.ToString();
				Session[sessionid] = query;
				string url = "AreaGoodsList.aspx";
				Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
			}
		}

		protected void btnExcel_Click(object sender, EventArgs e)
		{

			SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
			HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
			//var model = new HZ.Data.QueryModel();
            //model["AreaId"] = sq.AreaID;
            //model["DayTime"] = sq.Mid;
            string sortsource = Convert.ToString(sq.SortSource);
            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                //model["SortSorce"] = droSort.SelectedValue;
                sortsource = droSort.SelectedValue;
            }
            else
            {
                sortsource = "-1";
            }

			//model["SortFields"] = this.droSortField.SelectedValue;
            

			int t = 0;

			HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

			System.Data.DataTable dt = ie.ExecutePageFirst(1, 0, out t,  sortsource , droSortField.SelectedValue, sq.AreaID, sq.Mid);
			HZ.Supervise.Web.Function.AddColumn(dt, "SortSource");
			HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DataRate_PS");

			HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwSortInfo);
		}

		protected void btnQuery_Click(object sender, EventArgs e)
		{

            this.hidSortSource.Value = this.droSort.SelectedValue;

			BindList();

			GetStatistics();
		}

	}
}
